package com.mygenostics.misc;

import java.io.InputStream;
import java.util.Properties;

import org.apache.commons.lang.RandomStringUtils;
import org.apache.log4j.Logger;
import org.springframework.security.authentication.encoding.ShaPasswordEncoder;



public class Util {
	private static Logger log = Logger.getLogger(Util.class);
	
	public static Properties CONFIG = new Properties();
	
	static {
		try {
			InputStream inputStream = Util.class.getClassLoader()
        			.getResourceAsStream("mygenostics.properties");
			CONFIG.load(inputStream);
		}
		catch (Exception e) {
			log.fatal("Failed to load system property file mygenostics.properties. ", e);
			System.exit(0);
		}
	}
	
	/**
	 * Use MD5 to encrypt String
	 * @param str
	 * @return
	 */
	public static String encryptStrBySha(final String str) {
		if (str == null || str.length() == 0)
			return str;
		
		ShaPasswordEncoder encoder = new ShaPasswordEncoder();
		String pwd = encoder.encodePassword(str, null); 
		return pwd;
		
	}
	
	public static String generateRandomFileName() {
		
		String fileName = RandomStringUtils.random(20, true, true);
		
		return fileName;
	}
	
	public static String getFileExtension(String fileName) {
		if (fileName == null) return null;
		
		int mid= fileName.lastIndexOf(".");
		
		if (mid == -1) return "";
		
		return fileName.substring(mid+1,fileName.length());
	}
}
